Ext.BLANK_IMAGE_URL='/wsd/image/page/s.gif';
Ext.onReady(function(){
	var showType = 1;
	
	var usernameField = new Ext.form.TextField({
		name:'key',
		fieldLabel:'用户名'
	})
	
	var startDateField = new Ext.form.DateField({
		fieldLabel:'起始时间',
		emptyText:'请选择日期',
		editable:false,
		width:130
	})
	var endDateField = new Ext.form.DateField({
		fieldLabel:'结束时间',
		emptyText:'请选择日期',
		editable:false,
		width:130,
		value:new Date()
	})
	var searchBtn = new Ext.Button({
		text:'查找',
		handler:search
	})
	var searchPanel = new Ext.FormPanel({
		region:'north',
		height:60,
		frame:true,
		items:[usernameField,
			{
				layout:'column',
				items:[
					{
						layout:'form',
						width:250,
						items:[startDateField]
					},
					{
						layout:'form',
						width:250,
						items:[endDateField]
					},
					{
						layout:'form',
						//columnWidth:0.3,
						items:[searchBtn]
					}
				]
			}
		]
	})
	var sm = new Ext.grid.CheckboxSelectionModel();
	var s = new String();
	var cm = new Ext.grid.ColumnModel([
		sm,
		{header:'订单编号',dataIndex:'OId',width:60},
		{header:'下单日期',dataIndex:'OTime',type:'date',dateFormat:'Y-m-d H:i:s',width:120},
		{header:'用户名',dataIndex:'UName',renderer:function(value){
			var key = orderStore.baseParams['key'];
			return value.replace(key,'<font color="red">' + key + '</font>')}},
		{header:'用户类型',dataIndex:'userLevel',renderer:function(value){
			switch(value){
				case 1:
					value = '普通会员';
					break;
				case 2:
					value = '高级会员';
					break;
				case 3:
					value = 'VIP会员';
					break;
			}
			return value;
		}},
		{header:'商品总数',dataIndex:'OSumNum'},
		{header:'订单价格',dataIndex:'OSumPrice'},
		{header:'订单状态',dataIndex:'OState',renderer:function(value){
			return value?'已处理':'<font color="red">未处理</a>'
		}},
		{header:'详情',dataIndex:'OId',renderer:function(value){
			//alert(value)
			//return value
			var url = '<a href="admin/order_detail.action?oid=' + value + '" target="_blank">点击查看</a>';
			return url
		}}
	])
	
	var orderStore = new Ext.data.JsonStore({
		url:'/wsd/admin/order_search.action',
		totalProperty:'total',
		root:'result.orders',
		autoLoad:true,
		baseParams:{
			key:'',
			start:0,
			limit:10,
			startDate:null,
			endDate:null
		},
		fields:['OId','OTime','UName','OSumNum','OSumPrice','OState',
				{name:'UName',mapping:'users.UName'},
				{name:'userLevel',mapping:'users.userLevel'},
				'users'
			   ]
	})
	
	var orderPanel = new Ext.grid.GridPanel({
		region:'center',
		store:orderStore,
		viewConfig:{
			forceFit:true
		},
		loadMask:true,
		autoScroll:true,
		cm:cm,
		sm:sm,
		bbar:new Ext.PagingToolbar({
			pageSize:10,
			store:orderStore,
			displayInfo:true,
			displayMsg:'显示第 {0} 条到 {1} 条记录，一共 {2} 条',
			emptyMsg:'没有记录'
		}),
		tbar:new Ext.Toolbar([
			{
				text:'处理',
				handler:function(){
					var records = sm.getSelections();
					updateState(records,true);
					
				}
			},
			'|',
			{
				text:'撤销处理',
				handler:function(){
					var records = sm.getSelections();
					updateState(records,false);
				}
			
			}
		])
	})
	var orderListStore = new Ext.data.JsonStore({
		url:'/wsd/admin/order_getOrderList.action',
		root:'result.orderList',
		fields:['OId','UName','OSumNum','OSumPrice','OState',
				{name:'UName',mapping:'users.UName'},'users'
			   ]
	})
	orderPanel.on('rowdblclick',function(grid,rowIndex,e){
		//openOrderListWindow()
		
	})
	//中间主面板
	var panel = new Ext.Panel({
		region:'center',
		layout:'border',
		items:[orderPanel,searchPanel]
	})
	
	
	var viewport = new Ext.Viewport({
		layout : 'border',
		frame:true,
		items : [panel]
	});
	
	function updateState(records,state){
		var len = records.length;
		Ext.Msg.wait('正在处理，请稍后……');
		if(len == 0){
			Ext.Msg.alert('系统信息','请先选择订单');
			return false;
		}
		var OIds = new Array();
		for(var i = 0; i < len; i++){
			OIds.push(records[i].data.OId)
		}
		
		Ext.Ajax.request({
			url:'/wsd/admin/order_updateState.action',
			params:{
				OIds:OIds,
				state:state
			},
			success:function(response){
				Ext.Msg.hide();
				orderStore.reload();
				Ext.Msg.alert('系统信息','操作成功');
			},
			failure:function(){
				Ext.Msg.hide();
				Ext.Msg.alert('系统信息','操作失败');
			}
		})
	}
	/**
	 * 搜索
	 */
	function search(){
		var key = usernameField.getValue();
		if(key == null || key == ''){
			Ext.Msg.alert('系统信息','用户名不能为空');
			return false;
		}
		var startDate = startDateField.getValue();
		var endDate = endDateField.getValue();
		if(startDate == '' || endDate == ''){
			Ext.Msg.alert('系统信息','日期不能为空');
			return;
		}
		if(startDate > endDate){
			Ext.Msg.alert('系统信息','起始日期不能大于结束日期');
			return;
		}
		orderStore.baseParams['key'] = key;
		orderStore.baseParams['startDate'] = startDate;
		orderStore.baseParams['endDate'] = endDate;
		orderStore.load({
			params:{
				limit:10,
				start:0
			}
		})
	}
});